package com.youi.service.impl;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.dao.LeaderwithdrawDao;
import com.youi.entity.*;
import com.youi.manage.HttpSessionManage;
import com.youi.service.*;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Service
public class LeaderwithdrawServiceImpl extends BaseServiceImpl<Leaderwithdraw> implements LeaderwithdrawService{
		
		private LeaderwithdrawDao leaderwithdrawDao;
		
		@Autowired
		private GroupleaderService groupleaderService;
		@Autowired
		private LeaderprofitService leaderprofitService;
		@Autowired
		private LeaderstoreService leaderstoreService;
		@Autowired
		private SysuserService sysuserService;
		@Autowired
		private UserorderexcelService userorderexcelService;
		@Autowired
		public void setfaultRecordDao(LeaderwithdrawDao leaderwithdrawDao){
			this.leaderwithdrawDao=leaderwithdrawDao;
			super.setBaseDAO(leaderwithdrawDao);
		}

		@Override
		public List<Long> getLeaderIdsList(String pms) {
			// TODO Auto-generated method stub
			if (pms!=null&&pms.trim().length()>0) {
				List<Long> leaderIdList=new ArrayList<Long>();
				Map<String, Object> prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				Map<String, Object> searchParams=new HashMap<String, Object>();
				searchParams.put("phone", pms);
				searchParams.put("leaderId", pms);
				List<Leaderstore> leaderstoreList=leaderstoreService.getByParams(prams, searchParams, null);
				if (leaderstoreList!=null&&leaderstoreList.size()>0) {
					for (Leaderstore leaderstore : leaderstoreList) {
						leaderIdList.add(leaderstore.getLeaderId());
					}
					return leaderIdList;
				}
			}
			return null;
		}

		@Override
		public void getLeaderwithdraw(List<Leaderwithdraw> leaderwithdrawList) {
			// TODO Auto-generated method stub
			if (leaderwithdrawList!=null&&leaderwithdrawList.size()>0) {
				for (Leaderwithdraw leaderwithdraw : leaderwithdrawList) {
					getLeaderwithdraw(leaderwithdraw);
				}
			}
		}

		@Override
		public void getLeaderwithdraw(Leaderwithdraw leaderwithdraw) {
			// TODO Auto-generated method stub
			if (leaderwithdraw!=null) {
				Map<String, Object> prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put(Common.ID, leaderwithdraw.getLeaderId());
				Groupleader groupleader=groupleaderService.getByParam(prams);
				if (groupleader!=null) {
					leaderwithdraw.setName(groupleader.getName());
				}
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put("leaderId", leaderwithdraw.getLeaderId());
				Leaderprofit leaderprofit=leaderprofitService.getByParam(prams);
				if (leaderprofit!=null) {
					leaderwithdraw.setWithdres(leaderprofit.getWithdres());
				}
				Sysuser sysuser=sysuserService.get(leaderwithdraw.getAuditorId());
				if (sysuser!=null) {
					leaderwithdraw.setAuditorName(sysuser.getName());
				}
			}
		}

		@Override
		public void toExcelLeaderwithdraw(HttpServletRequest request, List<Leaderwithdraw> leaderwithdrawList) {
			// TODO Auto-generated method stub			
			try {
				Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
				String fileName="leaderwithdraw"+new Date().getTime()+""+SmsUtil.getRandom(3)+".xls";//文件名				
				String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
				WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
				WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
				WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
				WritableCellFormat format=new WritableCellFormat();
				format.setAlignment(Alignment.CENTRE);
				format.setVerticalAlignment(VerticalAlignment.CENTRE);
				Label label=new Label(0, 0, "提现单号", format);
				sheet1.addCell(label);
				label=new Label(1, 0, "期号", format);
				sheet1.addCell(label);
				label=new Label(2, 0, "团长编号", format);
				sheet1.addCell(label);
				label=new Label(3, 0, "认证门店号", format);
				sheet1.addCell(label);
				label=new Label(4, 0, "团长名称", format);
				sheet1.addCell(label);
				label=new Label(5, 0, "申请提现金额", format);
				sheet1.addCell(label);
				label=new Label(6, 0, "申请提现前的余额", format);
				sheet1.addCell(label);
				label=new Label(7, 0, "累计已提现佣金", format);
				sheet1.addCell(label);
				label=new Label(8, 0, "手续费", format);
				sheet1.addCell(label);
				label=new Label(9, 0, "实际到账金额", format);
				sheet1.addCell(label);
				label=new Label(10, 0, "提现方式", format);
				sheet1.addCell(label);
				label=new Label(11, 0, "审核状态", format);
				sheet1.addCell(label);
				label=new Label(12, 0, "审核人", format);
				sheet1.addCell(label);
				label=new Label(13, 0, "审核时间", format);
				sheet1.addCell(label);
				label=new Label(14, 0, "提现时间", format);
				sheet1.addCell(label);
				label=new Label(15, 0, "申请时间", format);
				sheet1.addCell(label);
				Leaderwithdraw leaderwithdraw=null;
				for (int i = 0,j=1; i < leaderwithdrawList.size(); i++,j++) {
					leaderwithdraw=leaderwithdrawList.get(i);
					ObjectUtil.ToChangeNullToEmpty(leaderwithdraw);
					label=new Label(0, j, leaderwithdraw.getOrderNo(), format);
					sheet1.addCell(label);
					label=new Label(1, j, leaderwithdraw.getDateNo(), format);
					sheet1.addCell(label);
					label=new Label(2, j, leaderwithdraw.getId().toString(), format);
					sheet1.addCell(label);
					label=new Label(3, j, leaderwithdraw.getBelongStoreCode(), format);
					sheet1.addCell(label);
					label=new Label(4, j, leaderwithdraw.getName(), format);
					sheet1.addCell(label);
					label=new Label(5, j, String.valueOf(leaderwithdraw.getAmount()), format);
					sheet1.addCell(label);
					label=new Label(6, j, String.valueOf(leaderwithdraw.getLeaderAmount()), format);
					sheet1.addCell(label);
					label=new Label(7, j, String.valueOf(leaderwithdraw.getWithdres()), format);
					sheet1.addCell(label);
					label=new Label(8, j, String.valueOf(leaderwithdraw.getCommission()), format);
					sheet1.addCell(label);
					label=new Label(9, j, String.valueOf(leaderwithdraw.getRealAnt()), format);
					sheet1.addCell(label);
					label=new Label(10, j, "云享代付", format);
					sheet1.addCell(label);
					label=new Label(11, j, ObjectUtil.getPlatformApplyStatus(leaderwithdraw.getStatus()), format);
					sheet1.addCell(label);
					label=new Label(12, j, leaderwithdraw.getAuditorName(), format);
					sheet1.addCell(label);
					if (leaderwithdraw.getGmtAudit()!=null) {
						label=new Label(13, j, DateUtil.simpdfyMdHms.format(leaderwithdraw.getGmtAudit()), format);
						sheet1.addCell(label);
					}
					if (leaderwithdraw.getGmtWithdraw()!=null) {
						label=new Label(14, j,  DateUtil.simpdfyMdHms.format(leaderwithdraw.getGmtWithdraw()), format);
						sheet1.addCell(label);
					}					
					label=new Label(15, j, DateUtil.simpdfyMdHms.format(leaderwithdraw.getGmtCreated()), format);
					sheet1.addCell(label);
				}
				workbook.write();
				workbook.close();
				InputStream inStream=new FileInputStream(fileUrl);
				byte[]	data = ObjectUtil.readInputStream(inStream);
				String dowmupload=OSSUtil.upload(data, ".xls");
				if (dowmupload!=null&&dowmupload.length()>0) {
					userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 7, dowmupload));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}

	@Override
	public List<Leaderwithdraw> getExportLeaderWithdraw(Map<String, Object> params) {
		List<Leaderwithdraw> leaderwithdrawList = leaderwithdrawDao.getExportLeaderWithdraw(params);
		leaderwithdrawList.stream().forEach(withdraw -> {
			withdraw.setStatusTxt(ObjectUtil.getJXGWithdrawalState(withdraw.getStatus()));
			withdraw.setWithdrawType("云享代付");
			if(withdraw.getGmtAudit()!=null){
				withdraw.setGmtAuditTxt(DateUtil.simpdfyMdHms.format(withdraw.getGmtAudit()));
			}
			if(withdraw.getGmtWithdraw()!=null){
				withdraw.setGmtWithdrawTxt(DateUtil.simpdfyMdHms.format(withdraw.getGmtWithdraw()));
			}
			if(withdraw.getGmtCreated()!=null){
				withdraw.setGmtCreatedTxt(DateUtil.simpdfyMdHms.format(withdraw.getGmtCreated()));
			}
		});
		return leaderwithdrawList;
	}
}